<template>
    <el-dialog
            :title="!dataForm.id ? '新增' : '修改'"
            :close-on-click-modal="false"
            :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
                 label-width="100px">
            <el-form-item label="公告标题" prop="title">
                <el-input v-model="dataForm.title" placeholder="公告标题"></el-input>
            </el-form-item>
            <el-form-item label="公告内容" prop="content">
                <el-input type="textarea" :autosize="{ minRows: 4, maxRows: 4}" placeholder="游泳是一门技巧"
                          maxlength="400" show-word-limit v-model="dataForm.content">
                </el-input>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
    </el-dialog>
</template>

<script>
export default {
    data() {
        return {
            visible: false,
            dataForm: {
                id: 0,
                senderId: '',
                title: '',
                content: '',
                createTime: ''
            },
            dataRule: {
                title: [
                    {required: true, message: '公告标题不能为空', trigger: 'blur'}
                ],
                content: [
                    {required: true, message: '公告内容不能为空', trigger: 'blur'}
                ],
            }
        }
    },
    methods: {
        init(id) {
            this.dataForm.id = id || 0
            this.visible = true
            this.$nextTick(() => {
                this.$refs['dataForm'].resetFields()
                if (this.dataForm.id) {
                    this.$http.post("/announce/searchAnnounceById",{
                        id: this.dataForm.id
                    }).then(resp => {
                        if (resp.data.code === 200) {
                            this.dataForm.title = resp.data.list.title
                            this.dataForm.content = resp.data.list.content
                        }else {
                            this.$message.error(resp.data.msg)
                        }
                    })
                }
            })
        },
        // 表单提交
        dataFormSubmit() {
            this.$refs['dataForm'].validate((valid) => {
                if (valid) {
                    let data = {
                        title: this.dataForm.title,
                        content: this.dataForm.content
                    };
                    this.$http.post("/announce/insert",data).then(resp => {
                        if (resp.data.code === 200) {
                            this.$message({
                                message: '操作成功',
                                type: 'success',
                                duration: 1500,
                            });
                            this.visible = false;
                            this.$emit('refreshDataList');
                        } else {
                            this.$message.error(resp.data.msg);
                            this.visible = false;
                        }
                    })
                }
            })
        }
    }
}
</script>
